package hhm.android.my

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.databinding.DataBindingUtil
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.my.databinding.ActivitySetPasswordBinding
import siau.android.base.HttpHelper
import siau.android.base.SBApplication
import siau.android.base.SBBaseActivity
import siau.android.http.model.NewPasswordModel

class SetPasswordActivity : SBBaseActivity() {

    lateinit var dataBinding: ActivitySetPasswordBinding
    private var showPassword1 = false
    private var showPassword2 = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_set_password)
        initTitleWithLine(getString(R.string.set_password))

        dataBinding.activitySetPasswordSave.setOnClickListener {
//            getTipDialog(
//                this, {
//
//                },
//                title = getString(R.string.tip),
//                content = getString(R.string.tip_content)
//            )
            if (dataBinding.activitySetPasswordPassword1.text.toString() == dataBinding.activitySetPasswordPassword2.text.toString()) {
                showLoadingFragment(R.id.activity_set_password_fl)
                val r = NewPasswordModel(dataBinding.activitySetPasswordPassword1.text.toString())
                HttpHelper().setPassword(r)?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                    SBApplication.userData.hasPassword = 1
                    removeLoadingFragment()
                    setResult(1001)
                    finish()
                }, {
                    removeLoadingFragment()
                    showToast(it.message)
                })
            } else {
                showToast(getString(R.string.two_password_is_not_same))
            }
        }

        dataBinding.activitySetPasswordPassword1Iv.setOnClickListener {
            showPassword1 = !showPassword1
            if (showPassword1) {
                dataBinding.activitySetPasswordPassword1.transformationMethod = HideReturnsTransformationMethod.getInstance()
                dataBinding.activitySetPasswordPassword1Iv.setImageResource(R.mipmap.can_see)
            } else {
                dataBinding.activitySetPasswordPassword1.transformationMethod = PasswordTransformationMethod.getInstance()
                dataBinding.activitySetPasswordPassword1Iv.setImageResource(R.mipmap.can_not_see)
            }
            dataBinding.activitySetPasswordPassword1.setSelection(dataBinding.activitySetPasswordPassword1.text.length)
        }

        dataBinding.activitySetPasswordPassword2Iv.setOnClickListener {
            showPassword2 = !showPassword2
            if (showPassword2) {
                dataBinding.activitySetPasswordPassword2.transformationMethod = HideReturnsTransformationMethod.getInstance()
                dataBinding.activitySetPasswordPassword2Iv.setImageResource(R.mipmap.can_see)
            } else {
                dataBinding.activitySetPasswordPassword2.transformationMethod = PasswordTransformationMethod.getInstance()
                dataBinding.activitySetPasswordPassword2Iv.setImageResource(R.mipmap.can_not_see)
            }
            dataBinding.activitySetPasswordPassword2.setSelection(dataBinding.activitySetPasswordPassword2.text.length)
        }


    }
}